package com.bird.business.controller;

import com.bird.business.annotation.SysLog;
import com.bird.business.domain.CostControl;
import com.bird.business.domain.CostControl;
import com.bird.business.domain.TbUser;
import com.bird.business.service.ICostControlService;
import com.bird.business.utils.FormatMapKey;
import com.bird.business.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangdan
 * @version 1.0
 * @date 2020/4/30 8:21
 */
@Controller
@RequestMapping("costControl")
public class CostControlController {

    @Resource
    private ICostControlService iCostControlService;


    @RequestMapping("selectByEntity")
    @ResponseBody
    public Map<String, Object> selectByEntity(@RequestParam Map<String, Object> paramMap, HttpServletRequest request) {
        String id = request.getParameter("id");
        String costName = request.getParameter("costName");
        String buyer = request.getParameter("buyer");
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");

        CostControl costControl = new CostControl();
        if (!StringUtils.isBlank(id)) {
            costControl.setId(Long.parseLong(id));
        }
        if (!StringUtils.isBlank(costName)) {
            costControl.setCostName(costName);
        }
        if (!StringUtils.isBlank(buyer)) {
            costControl.setBuyer(buyer);
        }
        //获取分页参数
        String page = org.springframework.util.StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
        String limit = org.springframework.util.StringUtils.isEmpty(request.getParameter("limit")) ? "999" : request.getParameter("limit");
        //设置分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<CostControl> costControls = this.iCostControlService.selectByEntity(costControl);
        //获取分页数据
        PageInfo<CostControl> pageInfo = new PageInfo<CostControl>(costControls);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", pageInfo.getTotal());
        resultMap.put("data", costControls);
        return resultMap;
    }

    @RequestMapping("selectByMap")
    @ResponseBody
    public Map<String, Object> selectByMap(@RequestParam Map<String, Object> paramMap, HttpServletRequest request) {
        String id = request.getParameter("id");
        String costName = request.getParameter("costName");
        String buyer = request.getParameter("buyer");
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isBlank(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isBlank(costName)) {
            map.put("costName",costName);
        }
        if (!StringUtils.isBlank(buyer)) {
            map.put("buyer",buyer);
        }
        if (!StringUtils.isBlank(starttime)) {
            map.put("starttime",starttime);
        }
        if (!StringUtils.isBlank(endtime)) {
            map.put("endtime",endtime);
        }
        //获取分页参数
        String page = org.springframework.util.StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
        String limit = org.springframework.util.StringUtils.isEmpty(request.getParameter("limit")) ? "999" : request.getParameter("limit");
        //设置分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<CostControl> costControls = this.iCostControlService.selectByMap(map);
        //获取分页数据
        PageInfo<CostControl> pageInfo = new PageInfo<CostControl>(costControls);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", pageInfo.getTotal());
        resultMap.put("data", costControls);
        return resultMap;
    }

    /**
     * 新增/修改 成本
     *
     * @param request
     */
    @SysLog(value = "新增成本/修改成本")
    @Transactional
    @RequestMapping(value = "/addOrUpdateCost", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil addOrUpdateCost(@RequestBody Map<String, String> paramMap, HttpServletRequest request) {
        //当前用户信息
        TbUser tbUsers = (TbUser) request.getSession().getAttribute("USER");
        Date date = new Date();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String id = paramMap.get("id");
        String costName = paramMap.get("costName");
        String costNum = paramMap.get("costNum");
        String costPrice = paramMap.get("costPrice");
        String sumPrice = paramMap.get("sumPrice");
        String purchaseTime = paramMap.get("purchaseTime");
        String buyer = paramMap.get("buyer");
        //id为空则增加，不为空则修改
        try {
            if (StringUtils.isBlank(id)) {
            //先插入，再查询
            CostControl costControl = new CostControl();
            costControl.setCostName(costName);
            costControl.setCostNum(Double.parseDouble(costNum));
            costControl.setCostPrice(Double.parseDouble(costPrice));
            costControl.setSumPrice(Double.parseDouble(sumPrice));
            costControl.setPurchaseTime(sdf.parse(purchaseTime));
            //costControl.setPurchaseTime(date);
            costControl.setBuyer(buyer);
            costControl.setCreateTime(date);
            costControl.setUpdateTime(date);
            iCostControlService.insertCost(costControl);
            //System.out.println("返回添加的hrBaseId" + wbBaseEntity.getHrBaseId());
            //request.getSession().setAttribute("hrBaseId", wbBaseEntity.getHrBaseId());
            return ResultUtil.ok();
            } else {
                CostControl costControl = new CostControl();
                costControl.setId(Long.parseLong(id));
                costControl.setCostName(costName);
                costControl.setCostNum(Double.parseDouble(costNum));
                costControl.setCostPrice(Double.parseDouble(costPrice));
                costControl.setSumPrice(Double.parseDouble(sumPrice));
                costControl.setPurchaseTime(sdf.parse(purchaseTime));
                //costControl.setPurchaseTime(date);
                costControl.setBuyer(buyer);
                //costControl.setCreateTime(date);
                costControl.setUpdateTime(date);
                iCostControlService.updateCost(costControl);
                return ResultUtil.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }



    /**
     * 删除成本信息
     *
     * @param id
     */
    @SysLog(value = "删除")
    @RequestMapping(value = "/deleteCost", method = RequestMethod.POST)
    @ResponseBody
    public ResultUtil deleteCost(@RequestParam Long id) {
        try {
            iCostControlService.deleteCost(id);
            return ResultUtil.ok("刪除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error();
    }
}
